package com.xyy.saas.payment.controller;

import com.xyy.saas.payment.dao.model.XydCheckDataQueryParam;
import com.xyy.saas.payment.dao.model.XydRepayPlanQueryParam;
import com.xyy.saas.payment.manage.cores.vo.ResultPageVo;
import com.xyy.saas.payment.manage.cores.vo.ResultVO;
import com.xyy.saas.payment.pojo.vo.XydCheckDataVo;
import com.xyy.saas.payment.pojo.vo.XydRepayPlanDetailVo;
import com.xyy.saas.payment.pojo.vo.XydRepayPlanVo;
import com.xyy.saas.payment.service.XydLoanService;
import com.xyy.saas.payment.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author liujf
 * 小雨点金融渠道界面
 */
@RestController
@RequestMapping("/xydLoan")
public class XydLoanController {

    @Autowired
    private XydLoanService xydLoanService;

    @PostMapping("/downFile")
    public ResultVO downFile(@RequestParam("fileType") String fileType, @RequestParam("checkDate") String checkDate, @RequestParam("checkCycle") String checkCycle) {
        xydLoanService.downloadAndSaveFinances(fileType, checkDate, checkCycle);
        return ResultVO.createSuccess();
    }

    @PostMapping("/account/list")
    public ResultPageVo<List<XydCheckDataVo>> accountList(@RequestBody XydCheckDataQueryParam param) {
        return xydLoanService.queryAccountList(param);
    }

    @PostMapping("/repayPlan/list")
    public ResultPageVo<List<XydRepayPlanVo>> repayPlanList(@RequestBody XydRepayPlanQueryParam param) {
        return xydLoanService.queryRepayPlanList(param);
    }

    @PostMapping("/repayPlan/detail")
    public ResultVO<List<XydRepayPlanDetailVo>> repayPlanDetail(@RequestParam("payNo") String payNo) {
        if (StringUtils.isEmpty(payNo)) {
            return ResultVO.createError("支付单号不能为空");
        }
        return ResultVO.createSuccess(xydLoanService.queryRepayPlanDetail(payNo));
    }

    @PostMapping(value = "/account/exportList")
    public void exportAccountList(@RequestBody XydCheckDataQueryParam param, HttpServletResponse response) {
        if (param.getTradeStartDate() == null || param.getTradeEndDate() == null || !DateUtil.isSameYearMonth(param.getTradeStartDate(), param.getTradeEndDate())) {
            throw new IllegalArgumentException("请选择交易日期在一个月份");
        }
        xydLoanService.exportAccountList(param, response);
    }

    @PostMapping(value = "/repayPlan/exportList")
    public void exportRepayPlanList(@RequestBody XydRepayPlanQueryParam param, HttpServletResponse response) {
        if (param.getPayStartTime() == null || param.getPayEndTime() == null || !DateUtil.isSameYearMonth(param.getPayStartTime(), param.getPayEndTime())) {
            throw new IllegalArgumentException("请选择支付时间在一个月份");
        }
        xydLoanService.exportRepayPlanList(param, response);

    }
}
